
mod commands;
mod utilty;
mod net;
mod cgroup;


use crate::commands::jdocker::Jdocker;
use crate::commands::ps::ps;
use crate::commands::tar_image::{tar, tar_image};
use crate::utilty::{log_error, log_info};
use commands::exec::exec;
use commands::kill::kill_container;
use commands::run::run;
use nix::libc::{exit, kill};
use std::env::args;
use std::io::{Read, Write};
use std::path::Path;
use crate::commands::exec::wrap_exec;
use crate::commands::images::images;
use crate::commands::net::init_net;
use crate::commands::rm_container::{rm, rm_container};
use crate::commands::run::wrap_run;
use crate::net::init_container_net::init_container_net;

///### 1 ***Do home command*** <br/>
///#### `jidocker homedir`<br/>
///### 2 ***Do tar command*** <br/>
///#### `jidocker tar /[path/]busybox.tar /var/jdocker/image_dir/<image_name>`<br/>
///### 3 ***Do run command*** <br/>
///#### `jidocker run <image_name> <container_name> <command ...>`<br/>
fn main() {
    if cfg!(feature = "release") {
        // 在main函数入口设置自定义panic钩子
        std::panic::set_hook(Box::new(|panic_info| {
            // 提取原始panic信息
            let msg = panic_info.to_string();

            if let Some((_,rest)) = msg.split_once("\n"){
                eprintln!("{}", rest);
                return;
            }
            // 其他panic保持原样输出
            eprintln!("{}", msg);
        }));
    }
    unsafe {
        match args().nth(1) {
            None => exit(1),
            Some(arg) => match arg.as_str().trim() {
                "exec" => wrap_exec(),
                "ps" => ps(),
                "run" => wrap_run(),
                "kill" => kill_container(),
                "images" => images(),
                "homedir" => home(),
                "net" => init_net(),
                "rm" => rm(),
                "tar" => tar(),
                _ => {
                    println!("unknown command")
                }
            },
        }
    }
}




fn home() {
    Jdocker::new().home_dir();
}
